package com.maozzi.web.controller.rest.back;

import com.maozzi.api.logger.annotation.SysLogger;
import com.maozzi.api.user.UserService;
import com.maozzi.api.user.model.UserDTO;
import com.maozzi.common.model.PageDTO;
import com.maozzi.common.model.PageParam;
import com.maozzi.common.model.PageVO;
import com.maozzi.common.model.ResultVO;
import com.maozzi.common.util.CloneUtil;
import com.maozzi.common.util.ParamCheckUtil;
import com.maozzi.web.auth.annotation.RequirePermissions;
import com.maozzi.web.model.front.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员 controller
 *
 * @author maozi
 */
@Slf4j
@RestController
@RequestMapping("/rest/back/admin")
@RequirePermissions(code = "admin-code")
public class AdminController {
    @Resource
    private UserService userService;

    /**
     * 分页查询管理员列表
     *
     * @param vo        查询条件
     * @param pageParam 分页参数
     * @return 返回
     */
    @GetMapping("/page.json")
    public PageVO<UserVO> page(UserVO vo, PageParam pageParam) {
        ParamCheckUtil.notNull(vo, "查询参数错误");
        ParamCheckUtil.notNull(pageParam, "分页参数错误");

        UserDTO dto = CloneUtil.to(vo, UserDTO.class);
        PageDTO<UserDTO> pageDTO = userService.adminPage(dto, pageParam);

        return CloneUtil.toPageVO(pageDTO, UserVO.class);
    }

    /**
     * 添加管理员
     *
     * @param userIds 用户ID
     * @return 返回
     */
    @SysLogger(message = "添加管理员")
    @PostMapping("/add.json")
    public ResultVO<Boolean> add(@RequestBody List<Long> userIds) {
        ParamCheckUtil.notNull(userIds, "参数错误");

        Boolean b = userService.addAdmin(userIds);

        return ResultVO.success(b);
    }

}
